<template>
  <div class="app-container">
    <el-card :bordered="false">
      <!-- 添加或修改公告对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="文章标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入文章标题"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="文章类型" prop="type">
              <el-select v-model="form.type.toString()" placeholder="请选择">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="文章封面图：" prop="image">
              <picture-manager
                width="150"
                height="150"
                :value="form.image"
              >
              </picture-manager>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容">
              <editor v-model="form.content" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="padding-left: 30px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import Editor from '@/components/Editor'
import PictureManager from '@/components/ImageUpload/PictureManager'
import * as articleApi from '@/api/shop/article'

export default {
  name: 'articleAdd',
  components: {
    Editor,
    PictureManager
  },
  data() {
    return {
      // 状态数据字典
      typeOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '文章类型不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    let id=this.$route.query.id;
    if (this.$route.query.hasOwnProperty("id")) {
      articleApi.articleList({id}).then((res)=>{
        this.form=res.rows[0];
        this.form.image=[this.form.image];
      })
    } else {
      this.reset()
    }
    this.getDicts('article_type').then(response => {
      this.typeOptions = response.data
    })
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        title: '',
        type: '0',
        image: [],
        content: ''
      }
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let data = Object.assign({}, this.form)
          data.image = this.form.image[0]
          articleApi.updateOrSave(data).then(response => {
            // 调用全局挂载的方法
            this.$store.dispatch('tagsView/delView', this.$route)
            // 返回上一步路由
            this.$router.go(-1)
          })
        }
      })
    }
  }
}
</script>
